//
//  PlaylistMigration.swift
//  hello
//
//  Created by 赵康 on 2025/4/13.
//

import Fluent

extension Playlist {
  struct Migration: AsyncMigration {
    var name: String { "PlaylistMigration" }
    
    func prepare(on database: any Database) async throws {
      try await database.schema(Playlist.schema)
        .id()
        .field(Playlist.FieldKeys.name, .string, .required)
        .field(Playlist.FieldKeys.description, .sql(.text))
        .field(Playlist.FieldKeys.cover, .string)
        .field(Playlist.FieldKeys.user, .uuid, .required, .references(User.schema, .id, onDelete: .cascade))
        .field(Playlist.FieldKeys.audioIds, .array(of: .uuid), .required)
        .field(Playlist.FieldKeys.audioAddedDates, .dictionary(of: .double), .required)
        .field(Playlist.FieldKeys.type, .string, .required)
        .field(Playlist.FieldKeys.sortOrder, .int, .required)
        .field(Playlist.FieldKeys.isDeleted, .bool, .required, .sql(.default(false)))
        .field(Playlist.FieldKeys.createdAt, .datetime, .required)
        .field(Playlist.FieldKeys.updatedAt, .datetime, .required)
        .create()
    }
    
    func revert(on database: any Database) async throws {
      try await database.schema(Playlist.schema).delete()
    }
  }
}

